/*
 * Copyright 2007 www.luzand.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Depends on: Configuration.js

var yrprxComponentsFactory = {};

yrprxComponentsFactory.componentExists = function(CID)
{
    try
    {
        return !!Components.classes[CID];
    }
    catch(ex)
    {
        return false;
    }
}

yrprxComponentsFactory.getStringBundleService = function()
{
    return Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
}

yrprxComponentsFactory.getPreferencesService = function()
{
    return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
}

yrprxComponentsFactory.getPreferencesServiceAsBranch = function()
{
    return this.getPreferencesService().QueryInterface(Components.interfaces.nsIPrefBranch);
}

yrprxComponentsFactory.createScriptableUnicodeConverter = function(charset)
{
    if (!charset) charset = "UTF-8";

    var converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Components.interfaces.nsIScriptableUnicodeConverter);

    converter.charset = charset;

    return converter;
}

yrprxComponentsFactory.createStringInputStream = function(value)
{
    var stringInputStream = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream);

    //TODO: check it
    if ("data" in stringInputStream) // Gecko 1.9 or newer
        stringInputStream.data = value;
    else // 1.8 or older
        stringInputStream.setData(value, value.length);

    return stringInputStream;
}

yrprxComponentsFactory.createScriptableInputStream = function(inputStream)
{
    var scriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);

    scriptableInputStream.init(inputStream);

    return scriptableInputStream; 
}

yrprxComponentsFactory.getUTF8ConverterService = function()
{
    return Components.classes["@mozilla.org/intl/utf8converterservice;1"].getService(Components.interfaces.nsIUTF8ConverterService);
}

yrprxComponentsFactory.createConverterOutputStream = function(outStream, charset, bufferSize, replacementCharacter)
{
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].createInstance(Components.interfaces.nsIConverterOutputStream);

    if (outStream)
    {
        if (!charset) charset = "UTF-8";
        if (typeof bufferSize == "undefined") bufferSize = 0;
        if (typeof replacementCharacter == "undefined") replacementCharacter = '?';

        converter.init(outStream, charset, bufferSize, replacementCharacter);
    }

    return converter.QueryInterface(Components.interfaces.nsIUnicharOutputStream);
}

yrprxComponentsFactory.createDOMParser = function()
{
    return Components.classes["@mozilla.org/xmlextras/domparser;1"].createInstance(Components.interfaces.nsIDOMParser);
}

yrprxComponentsFactory.createXMLDocument = function()
{
    return Components.classes["@mozilla.org/xml/xml-document;1"].createInstance(Components.interfaces.nsIDOMXMLDocument);
}

yrprxComponentsFactory.implementsIDOMXMLDocument = function(obj)
{
    return (obj instanceof Components.interfaces.nsIDOMXMLDocument);
}

yrprxComponentsFactory.createXMLSerializer = function()
{
    return Components.classes["@mozilla.org/xmlextras/xmlserializer;1"].createInstance(Components.interfaces.nsIDOMSerializer);
}

yrprxComponentsFactory.createXMLHttpRequest = function()
{
    return Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance();
}

yrprxComponentsFactory.getIOService = function()
{
    return Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
}

yrprxComponentsFactory.createChannel = function(spec, originCharset, baseURI)
{
    return this.getIOService().newChannel(spec, originCharset ? originCharset : null, baseURI ? baseURI : null);
}

yrprxComponentsFactory.createSimpleStreamListener = function(sink, observer)
{
    var listener = Components.classes["@mozilla.org/network/simple-stream-listener;1"].createInstance(Components.interfaces.nsISimpleStreamListener);

    listener.init(sink, observer);

    return listener;
}

yrprxComponentsFactory.getMIMEService = function()
{
    return Components.classes["@mozilla.org/mime;1"].getService(Components.interfaces.nsIMIMEService);
}

yrprxComponentsFactory.createLocalFile = function(path)
{
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

    if (typeof path != "undefined") file.initWithPath(path);

    return file;
}

yrprxComponentsFactory.implementsIFile = function(obj)
{
    return (obj instanceof Components.interfaces.nsIFile);
}

yrprxComponentsFactory.createFilePicker = function()
{
    return Components.classes["@mozilla.org/filepicker;1"].createInstance(Components.interfaces.nsIFilePicker);
}

/*
Open flags:

PR_RDONLY       0x01
PR_WRONLY       0x02
PR_RDWR         0x04
PR_CREATE_FILE  0x08
PR_APPEND       0x10
PR_TRUNCATE     0x20
PR_SYNC         0x40
PR_EXCL         0x80

File modes:

CAVEAT: 'mode' is currently only applicable on UNIX platforms.
The 'mode' argument may be ignored by PR_Open on other platforms.

00400   Read by owner.
00200   Write by owner.
00100   Execute (search if a directory) by owner.
00040   Read by group.
00020   Write by group.
00010   Execute by group.
00004   Read by others.
00002   Write by others
00001   Execute by others.
*/
yrprxComponentsFactory.createFileInputStream = function(file, ioFlags, perm, behaviorFlags)
{
    var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"].createInstance(Components.interfaces.nsIFileInputStream);

    if (typeof ioFlags == "undefined") ioFlags = 0x01;
    if (typeof perm    == "undefined") perm    = 0x00;
    if (typeof behaviorFlags == "undefined") behaviorFlags = 0x00;

    inputStream.init(file, ioFlags, perm, behaviorFlags);

    return inputStream;
}

yrprxComponentsFactory.implementsIInputStream = function(obj)
{
    return (obj instanceof Components.interfaces.nsIInputStream);
}

yrprxComponentsFactory.createFileOutputStream = function(file, ioFlags, perm, behaviorFlags)
{
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);

    if (typeof ioFlags == "undefined") ioFlags = 0x02 | 0x08 | 0x20;
    if (typeof perm    == "undefined") perm    = 0666;
    if (typeof behaviorFlags == "undefined") behaviorFlags = 0x00;

    outputStream.init(file, ioFlags, perm, behaviorFlags);

    return outputStream;
}

yrprxComponentsFactory.createSafeFileOutputStream = function(file, ioFlags, perm, behaviorFlags)
{
    var outputStream = Components.classes["@mozilla.org/network/safe-file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);

    if (typeof ioFlags == "undefined") ioFlags = 0x02 | 0x08 | 0x20;
    if (typeof perm    == "undefined") perm    = 0666;
    if (typeof behaviorFlags == "undefined") behaviorFlags = 0x00;

    outputStream.init(file, ioFlags, perm, behaviorFlags);

    return outputStream;
}

yrprxComponentsFactory.implementsIInputStream = function(obj)
{
    return (obj instanceof Components.interfaces.nsISafeOutputStream);
}

yrprxComponentsFactory.createBufferedInputStream = function(inputStream, buffer)
{
    if (inputStream instanceof Components.interfaces.nsIBufferedInputStream)
        return inputStream;

    var bufferedInputStream = Components.classes["@mozilla.org/network/buffered-input-stream;1"].createInstance(Components.interfaces.nsIBufferedInputStream);

    bufferedInputStream.init(inputStream, !buffer ? GGC_yrprxConfig.getStreamBufferSize() : buffer);

    return bufferedInputStream;
}

yrprxComponentsFactory.createBufferedOutputStream = function(outputStream, buffer)
{
    if (outputStream instanceof Components.interfaces.nsIBufferedOutputStream)
        return outputStream;

    var bufferedOutputStream = Components.classes["@mozilla.org/network/buffered-output-stream;1"].createInstance(Components.interfaces.nsIBufferedOutputStream);

    bufferedOutputStream.init(outputStream, !buffer ? GGC_yrprxConfig.getStreamBufferSize() : buffer);

    return bufferedOutputStream;
}

yrprxComponentsFactory.createStorageStream = function(maxSize)
{
    var storageStream = Components.classes["@mozilla.org/storagestream;1"].createInstance(Components.interfaces.nsIStorageStream);

    storageStream.init(GGC_yrprxConfig.getStreamBufferSize(), !maxSize ? -1 : maxSize , null);

    return storageStream;
}

yrprxComponentsFactory.createMultiplexInputStream = function()
{
    return Components.classes["@mozilla.org/io/multiplex-input-stream;1"].createInstance(Components.interfaces.nsIMultiplexInputStream);
}

//yrprxComponentsFactory.createProcess = function(file)
//{
//    var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
//
//    if (typeof file != "undefined") process.init(file);
//
//    return process;
//}
//
//yrprxComponentsFactory.createProcessByPath = function(path)
//{
//    return this.createProcess(this.createLocalFile(path));
//}
//
//yrprxComponentsFactory.createProcessByPath = function(path)
//{
//    return this.createProcess(this.createLocalFile(path));
//}

yrprxComponentsFactory.createWebBrowserPersist = function()
{
    return Components.classes['@mozilla.org/embedding/browser/nsWebBrowserPersist;1'].createInstance(Components.interfaces.nsIWebBrowserPersist);
}

yrprxComponentsFactory.getURIFixupService = function()
{
    return Components.classes['@mozilla.org/docshell/urifixup;1'].getService(Components.interfaces.nsIURIFixup);
}

yrprxComponentsFactory.getImgCacheService = function()
{
    return Components.classes["@mozilla.org/image/cache;1"].getService(Components.interfaces.imgICache);
}

yrprxComponentsFactory.getDirectoryServiceAsProperties = function()
{
    return Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
}

yrprxComponentsFactory.getStorageService = function()
{
    return Components.classes["@mozilla.org/storage/service;1"].getService(Components.interfaces.mozIStorageService);
}

yrprxComponentsFactory.getWindowMediatorService = function()
{
    return Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
}

yrprxComponentsFactory.getWindowWatcherService = function()
{
    return Components.classes["@mozilla.org/embedcomp/window-watcher;1"].getService(Components.interfaces.nsIWindowWatcher);
}

yrprxComponentsFactory.getPromptService = function()
{
    return Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
}

yrprxComponentsFactory.getCookieManager = function()
{
    return Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager2);
}

yrprxComponentsFactory.getObserverService = function()
{
    return Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService);
}

yrprxComponentsFactory.getDragService = function()
{
    return Components.classes["@mozilla.org/widget/dragservice;1"].getService(Components.interfaces.nsIDragService);
}

yrprxComponentsFactory.getPasswordManager = function()
{
    var service = Components.classes["@mozilla.org/passwordmanager;1"].getService(Components.interfaces.nsIPasswordManager);

    return service.QueryInterface(Components.interfaces.nsIPasswordManagerInternal);
}

yrprxComponentsFactory.createTransferable = function()
{
    return Components.classes["@mozilla.org/widget/transferable;1"].createInstance(Components.interfaces.nsITransferable);
}

yrprxComponentsFactory.getClipboardService = function()
{
    return Components.classes["@mozilla.org/widget/clipboard;1"]. getService(Components.interfaces.nsIClipboard);
}

//yrprxComponentsFactory.getThreadManager = function()
//{
//    return Components.classes["@mozilla.org/thread-manager;1"].getService(Components.interfaces.nsIThreadManager);
//}
//
//yrprxComponentsFactory.createNewThread = function()
//{
//    return this.getThreadManager().newThread(0);
//}

yrprxComponentsFactory.createTimer = function(aCallback, aDelay, aType)
{
    var timer = Components.classes["@mozilla.org/timer;1"].createInstance(Components.interfaces.nsITimer);

    if (typeof aCallback == "undefined")
        return timer;

    if (typeof aType == "undefined")
        aType = Components.interfaces.nsITimer.TYPE_ONE_SHOT;

    timer.initWithCallback(aCallback, aDelay, aType);

    return timer;
}

yrprxComponentsFactory.getAppInfo = function()
{
    return Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULAppInfo);
}

yrprxComponentsFactory.getVersionComparator = function()
{
    return Components.classes["@mozilla.org/xpcom/version-comparator;1"].getService(Components.interfaces.nsIVersionComparator);
}
